import {
    AfterViewChecked,
    Component,
    ElementRef,
    EventEmitter,
    Injector,
    Output,
    ViewChild,
} from "@angular/core";
import { AppConsts } from "@shared/AppConsts";
import { AppComponentBase } from "@shared/common/app-component-base";
import {
    LineInstanceServiceProxy,
    ProcessItemList,
} from "@shared/service-proxies/service-proxies";
import { ModalDirective } from "ngx-bootstrap";
import * as moment from "moment";
import * as _ from "lodash";
import { finalize } from "rxjs/operators";
import { Steps } from "primeng/primeng";
import { forEach } from "@angular/router/src/utils/collection";
import { stringify } from "querystring";

@Component({
    selector: "processitemModal",
    templateUrl: "./processitem-modal.component.html",
})
export class ProcessitemModalComponent extends AppComponentBase {
    @ViewChild("processitemModal") modal: ModalDirective;

    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    active = false;
    LineInstanceId: string;
    statusData: any[];
    tt = {
        step: "",
        status: "",
    };
    //ProcessItems: ProcessItemList;
    ProcessItems = new ProcessItemList();

    constructor(
        injector: Injector,
        private _lineInstanceServiceProxy: LineInstanceServiceProxy
    ) {
        super(injector);
    }

    show(LineInstanceId: string) {
        this.active = true;
        this.LineInstanceId = LineInstanceId;
        this.ProcessItems.productionDrawingCode = "";

        this._lineInstanceServiceProxy
            .getProcessItem(LineInstanceId)
            .subscribe((result) => {
                this.ProcessItems = result;
                this.statusData = [];

                this.ProcessItems.getProcessItemOutputs.forEach((element) => {
                    let time = moment(element.startTime).format("lll");
                    let name = element.user;
                    let gw = element.nodeName;

                    if (
                        this.ProcessItems.getProcessItemOutputs.indexOf(
                            element
                        ) ==
                        this.ProcessItems.getProcessItemOutputs.length - 1
                    ) {
                        let step =
                            "工位:" + gw + ",人员:" + name + ",时间:" + time;
                        let status = "In progress";

                        let tt = {
                            step: step,
                            status: status,
                        };
                        this.statusData.push(tt);
                    } else {
                        let step =
                            "工位: " +
                            gw +
                            ", 人员: " +
                            name +
                            ",时间: " +
                            time;

                        let status = "Completed";
                        let tt = {
                            step: step,
                            status: status,
                        };
                        this.statusData.push(tt);
                    }
                    this.modal.show();
                });

                // console.log(this.statusData);
            });
    }

    close(): void {
        //this.active = false;
        this.modal.hide();
    }
}
